﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using Maticsoft.DBUtility;
using Utils;
using System.Collections.Specialized;
using Maticsoft.Model;


public partial class vote_index : FBasePage 
{
    // Fields


    private bool result = false;
    public string strAbbreviation = string.Empty;


    // Methods
    private void initList()
    {
        if (!base.User.Identity.IsAuthenticated)
        {
            base.Response.Redirect("../account/Login.aspx");
        }
        StringBuilder builder = new StringBuilder();
        DataSet list = new  Maticsoft.BLL.vote_question().GetList(" is_show=1 order by [order] desc");
        if (list.Tables.Count > 0)
        {
            if (list.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < list.Tables[0].Rows.Count; i++)
                {
                    DataRow row = list.Tables[0].Rows[i];
                    string str = row["Type"].ToString();
                    string str2 = row["Title"].ToString();
                    string str3 = row["ID"].ToString();
                    int num3 = i + 1;
                    builder.Append("<li id=\"vote-list-" + num3.ToString() + "\">");
                    string[] strArray = new string[] { "<h4 id=\"title", str3, "\">", (i + 1).ToString(), "、", str2, "<input type='hidden' id='q' name='q' value='", str3, "'></h4>" };
                    builder.Append(string.Concat(strArray));
                    builder.Append("<div class=\"choices\">");
                    DataSet set2 = new Maticsoft.BLL.vote_options().GetList(" question_id=" + str3 + " and is_show=1 order by [order]");
                    if ((set2.Tables.Count > 0) && (set2.Tables[0].Rows.Count > 0))
                    {
                        for (int j = 0; j < set2.Tables[0].Rows.Count; j++)
                        {
                            DataRow row2 = set2.Tables[0].Rows[j];
                            string str4 = row2["name"].ToString();
                            string str5 = row2["is_br"].ToString();
                            string str6 = row2["is_input"].ToString();
                            string str7 = row2["ID"].ToString();
                            builder.Append("<input type=\"" + str + "\" value=\"" + str7 + "\"  name=\"op" + str3 + "\" class=\"choice\"/>");
                            builder.Append("<label class=\"text\" for=\"label-" + str3 + "-" + str7 + "\">" + str4 + "</label>");
                            if (str6 == "1")
                            {
                                builder.Append("<input type=\"text\" class=\"f-text\" name=\"txt" + str7 + "\" id=\"input" + str7 + "\" onfocus=\"document.getElementById('label-" + str3 + "-" + str7 + "').checked = true;\" onblur=\"document.getElementById('label-" + str3 + "-" + str7 + "').checked = this.value ? true : false;\">");
                            }
                            if (str5 == "1")
                            {
                                builder.Append("<br>");
                            }
                        }
                    }
                    builder.Append("</div>");
                    builder.Append("</li>");
                }
            }
            else
            {
                builder.Append("暂无调查！");
                this.submit.Visible = false;
            }
        }
        this.ltQuesstion.Text = builder.ToString();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (base.ASSystem != null)
        {
            this.strAbbreviation = base.ASSystem.abbreviation;
        }
        if (!this.Page.IsPostBack)
        {
            this.initList();
        }
    }

    protected void submit_ServerClick(object sender, EventArgs e)
    {
        int num = 0;
        string name = this.Page.User.Identity.Name;
        Maticsoft.BLL. UserInfo info = new Maticsoft.BLL.UserInfo();
        string strWhere = "";
        strWhere = string.Format(" Username='{0}'", name);
        DataSet list = info.GetList(strWhere);
        if ((list.Tables.Count > 0) && (list.Tables[0].Rows.Count > 0))
        {
            num = int.Parse(list.Tables[0].Rows[0]["id"].ToString());
        }
        Maticsoft.BLL.vote_feedback_question _question = new Maticsoft.BLL.vote_feedback_question();
        string[] strArray = base.Request.Form["q"].ToString().Split(new char[] { ',' });
        for (int i = 0; i < strArray.Length; i++)
        {
            int num3 = int.Parse(strArray[i]);
            if (base.Request.Form["op" + num3] != null)
            {
              Maticsoft.BLL.vote_feedback_question _question2 = new Maticsoft.BLL.vote_feedback_question();
                if (_question2.GetModelList(string.Concat(new object[] { "feedback_id=", num, " and question_id=", num3 })).Count > 0)
                {
                    this.result = true;
                }
                if (!this.result)
                {
                    string[] strArray2 = base.Request.Form["op" + num3].ToString().Split(new char[] { ',' });
                    for (int j = 0; j < strArray2.Length; j++)
                    {
                        vote_feedback_question _question3 = new vote_feedback_question();
                        _question3.question_id = num3;
                        _question3.feedback_id = num;
                        _question3.options_id = Convert.ToInt32(strArray2[j].ToString());
                        _question.Add(_question3);
                        if ((base.Request.Form["txt" + strArray2[j]] != null) && (base.Request.Form["txt" + strArray2[j]].ToString() != ""))
                        {
                            string str4 = base.Request.Form["txt" + strArray2[j]].ToString();
                           Maticsoft.BLL.vote_feedback_input _input = new Maticsoft.BLL.vote_feedback_input();
                            vote_feedback_input _input2 = new vote_feedback_input();
                            _input2.feedback_id = num;
                            _input2.options_id = Convert.ToInt32(strArray2[j].ToString());
                            _input2.value = str4;
                            _input.Add(_input2);
                        }
                    }
                   Maticsoft.BLL.vote_feedback _feedback = new Maticsoft.BLL.vote_feedback();
                    vote_feedback model = new vote_feedback();
                    model.Addtime = DateTime.Now;
                    model.User_id = num;
                    model.Username = name;
                    model.Ip = HttpContext.Current.Request.UserHostAddress;
                    _feedback.Add(model);
                    base.SetSuccess("您的调查结果已提交！");
                    base.Response.Redirect("index.aspx");
                }
                else
                {
                    this.SetError("您已经参与过调查!");
                    base.Response.Redirect("index.aspx");
                }
            }
        }
    }

}